#include <bits/stdc++.h>
// 2025/05/17
// tag: LCA
// Author: Forever
using namespace std;
#define MAXN 100001
#define LOG_MAX 20

int n, m, s;
vector<int> edge[MAXN], wei[MAXN];

int up[MAXN][LOG_MAX], cost[MAXN][LOG_MAX], depth[MAXN];

void preprocess(int u, int parent)
{
    up[u][0] = parent;
    for (int k = 1; k < LOG_MAX; ++k) 
        up[u][k] = up[up[u][k - 1]][k - 1], cost[u][k] = cost[u][k - 1] ^ cost[up[u][k - 1]][k - 1];
    for (int i = 0; i < edge[u].size(); ++i) {
        int v = edge[u][i], w = wei[u][i];
        if (v != parent) {
            depth[v] = depth[u] + 1, cost[v][0] = w;
            preprocess(v, u);
        }
    }
}

int lca(int u, int v)
{
    if (depth[u] < depth[v])
        swap(u, v);
    int res = 0;
    for (int k = LOG_MAX - 1; k >= 0; --k) 
        if (depth[u] - (1 << k) >= depth[v]) 
            res ^= cost[u][k], u = up[u][k];
    if (u == v)
        return res;
    for (int k = LOG_MAX - 1; k >= 0; --k) {
        if (up[u][k] != up[v][k]) {
            res ^= cost[u][k] ^ cost[v][k];
            u = up[u][k], v = up[v][k];
        }
    }
    return res ^ cost[u][0] ^ cost[v][0];
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        edge[u].push_back(v);
        edge[v].push_back(u);
        wei[u].push_back(w);
        wei[v].push_back(w);
    }
    preprocess(1, 1);
    cin >> m;
    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        int l = lca(u, v);
        cout << l << endl;
    }
    return 0;
}